<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="file" name="" id="file">
    <button id="btn">upload</button>
    <script>
        const btn = document.querySelector('#btn');
        btn.addEventListener('click', () => {
            const { files } = document.querySelector('#file');

            const xhr = new XMLHttpRequest();
            xhr.addEventListener('readystatechange', () => {
                if (xhr.readyState === 4) {
                    const res = {
                        status: xhr.status,
                        data: JSON.parse(xhr.responseText),
                    };
                    console.log('success');
                }
            });
            xhr.upload.addEventListener('progress', (e) => {
                if (e.lengthComputable) {
                    console.log(e.loaded / e.total);
                }
            });
            const BASE_URL = 'http://www.liulongbin.top:3006';
            xhr.open('POST', BASE_URL + '/api/upload/avatar');
            // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            const fd = new FormData();
            fd.append('avatar', files[0]);
            xhr.send(fd);
        });
        
    </script>
</body>
</html>